package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.ribbon.proxy.annotation.Http;
import com.vehicle.bean.Category;
import com.vehicle.mapper.CategoryMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.CategoryService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.FilterVO;
import com.vehicle.vo.ListCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    UserLogMapper userLogMapper;

    @Override
    public Category addCategoryByName(String ip,Integer id,String categoryName) {
        Category category = new Category();
        category.setCategoryName(categoryName);
        int b = categoryMapper.insert(category);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加车辆类型成功",ip,"成功"));
            return category;
        }
        userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加车辆类型失败",ip,"失败"));
        return null;
    }

    @Override
    public boolean deleteCategoryById(String ip,Integer id,Integer categoryId) {
        int b = categoryMapper.deleteById(categoryId);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除车辆类型ID["+categoryId+"]成功",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除车辆类型ID["+categoryId+"]失败",ip,"失败"));
        return false;
    }

    @Override
    public Integer getIdByName(String name) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name",name);
        Category category = categoryMapper.selectOne(categoryQueryWrapper);
        return category.getCategoryId();
    }

    @Override
    public List<FilterVO> getAllCategoryName() {
        List<String> list = categoryMapper.getAllCategoryName();
        List<FilterVO> filterVOS = new ArrayList<>();
        for (String ss:list) {
            FilterVO filterVO = new FilterVO();
            filterVO.setText(ss);
            filterVO.setValue(ss);
            filterVOS.add(filterVO);
        }
        return filterVOS;
    }

    @Override
    public ListCategoryVO getAllCategory(Integer page, Integer size) {
        Page<Category> infoPage = new Page<>(page,size);
        Page<Category> categoryPage = categoryMapper.selectPage(infoPage,null);
        ListCategoryVO listCategoryVO = new ListCategoryVO();
        listCategoryVO.setSize(categoryPage.getSize());
        listCategoryVO.setTotal(categoryPage.getTotal());
        listCategoryVO.setContent(categoryPage.getRecords());
        return listCategoryVO;
    }

    @Override
    public String getNameById(Integer id) {
        String name = categoryMapper.getCategoryNameById(id);
        return name;
    }
}
